// EventParms.cpp : implementation file
//

#include "stdafx.h"
#include "EventNotify.h"
#include "EventParams.h"


IMPLEMENT_SERIAL(CEventParams, CObject, ID_SERIALIZATION_VER)

// CEventParams

CEventParams::CEventParams()
	: m_strName(_T(""))
	, m_strMessage(_T(""))
	, m_iType(0)
	, m_tTime(0)
	, m_tsResolution(0)
	, m_byActiveDays(0)
	, m_bIsActive(false)
	, m_bLaunch(false)
	, m_iSoundID(0)
{
}

CEventParams::~CEventParams()
{
}


// CEventParams member functions


CString CEventParams::Name(void) const
{
	return m_strName;
}


void CEventParams::Name(const CString& strName)
{
	m_strName = strName;
}


CString CEventParams::Message(void) const
{
	return m_strMessage;
}


void CEventParams::Message(const CString& strMsg)
{
	m_strMessage = strMsg;
}


int CEventParams::EventType(void) const
{
	return m_iType;
}


void CEventParams::EventType(const int iType)
{
	ASSERT(iType == EVENT_TYPE_SCHEDULE || iType == EVENT_TYPE_TIMER);
		m_iType = iType;
}


CTime CEventParams::Time(void) const
{
	return m_tTime;
}


void CEventParams::Time(const CTime& tTime)
{
	CTime tNow = CTime::GetCurrentTime();
	m_tTime = CTime(tNow.GetYear(), tNow.GetMonth(), tNow.GetDay(), tTime.GetHour(), tTime.GetMinute(), 0);
}


CTimeSpan CEventParams::TimeSpan(void) const
{
	return m_tsResolution;
}


void CEventParams::TimeSpan(const CTimeSpan& tsRes)
{
	m_tsResolution = tsRes;
}


BYTE CEventParams::ActiveDays(void) const
{
	return m_byActiveDays;
}


void CEventParams::ActiveDays(const BYTE byActiveDays)
{
	m_byActiveDays = byActiveDays;
}


bool CEventParams::Active(void) const
{
	return m_bIsActive;
}


void CEventParams::Active(const bool bActive)
{
	m_bIsActive = bActive;
}


int CEventParams::SoundID(void) const
{
	return m_iSoundID;
}


void CEventParams::SoundID(const int iSoundID)
{
	m_iSoundID = iSoundID;
}


bool CEventParams::GetLaunchFlag(void) const
{
	return m_bLaunch;
}


void CEventParams::SetLaunchFlag(const bool bLaunch)
{
	m_bLaunch = bLaunch;
}


void CEventParams::Launch(void)
{
	CTime tNow = CTime::GetCurrentTime();

	if(m_iType == EVENT_TYPE_SCHEDULE)
	{
		while((tNow >= m_tTime) || (!(m_byActiveDays & (USE_DAY_SUN << m_tTime.GetDayOfWeek() - 1))))
		{
			int iDayMask = 1;
			int nAddDays = 1;

			iDayMask = iDayMask << (m_tTime.GetDayOfWeek() - 1);

			for(; nAddDays < 7; nAddDays++)
			{
				iDayMask = iDayMask << 1;
				if(iDayMask > USE_DAY_SAT) iDayMask = 1;
				if((iDayMask & m_byActiveDays) != 0)
					break;
			}

			if(nAddDays <= 7)
			{
				m_tTime += CTimeSpan(nAddDays, 0, 0, 0);
				m_bLaunch = true;
			}
		}
	}
	else if(m_iType == EVENT_TYPE_TIMER)
	{
		while((tNow >= m_tTime) || (!(m_byActiveDays & (USE_DAY_SUN << m_tTime.GetDayOfWeek() - 1))))
		{
			m_tTime += m_tsResolution;
			m_bLaunch = true;
		}
	}
}


void CEventParams::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{	// storing code
		ar << m_strName << m_strMessage << m_iType << m_tTime << m_tsResolution << m_byActiveDays << m_bIsActive << m_bLaunch << m_iSoundID;
	}
	else
	{	// loading code
		ar >> m_strName >> m_strMessage >> m_iType >> m_tTime >> m_tsResolution >> m_byActiveDays >> m_bIsActive >> m_bLaunch >> m_iSoundID;

		Launch();
	}
}
